package com.ruoyi.project.drug.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.drug.domain.Factories;
import com.ruoyi.project.drug.service.IFactoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spi.service.contexts.SecurityContext;

import java.util.List;

@RestController
@RequestMapping("/drug/factories")
public class FactoriesController extends BaseController {
    @Autowired
    private IFactoriesService iFactoriesService;
    /**查询所有生产厂*/
    @PreAuthorize("@ss.hasPermi('drug:factories:query')")
    @GetMapping("/list")
    public TableDataInfo list(Factories factories){
        startPage();
        List<Factories> list = iFactoriesService.getFactories(factories);
        //父类方法直接调用
        return getDataTable(list);
    }

    /**新增生产厂*/
    @PreAuthorize("@ss.hasPermi('drug:factories:add')")
    @PostMapping("/add") //前端没有给定具体的地址,所以这里不需要给路径
    public AjaxResult addFactories(@RequestBody Factories factories){
        //因为实体类继承了父类,所以在这种情况下,用对象调用父类方法直接存储名字
        factories.setCreateBy(SecurityUtils.getUsername());
        int rows =iFactoriesService.insertFactories(factories);
        //控制器继承BaseController,可以使用父类的Ajax的方法响应返回值
        return toAjax(rows);
    }

    /**获取生产厂信息*/
    @GetMapping("/{facId}")
    @PreAuthorize("@ss.hasPermi('drug:factories:query')")
    public AjaxResult getById(@PathVariable Long facId){
        return AjaxResult.success(iFactoriesService.getById(facId));
    }

    /**修改生产厂信息*/
    @PutMapping
    @PreAuthorize("@ss.hasPermi('drug:factories:edit')")
    public AjaxResult editFactories(@RequestBody Factories factories){
        //设置修改人的名字
        factories.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(iFactoriesService.updateFactories(factories));
    }

    /**删除生产厂信息*/
    @DeleteMapping("/{facIds}")
    @PreAuthorize("@ss.hasPermi('drug:factories:remove')")
    public AjaxResult removeFactories(@PathVariable Long[] facIds){
        return toAjax(iFactoriesService.deleteFactories(facIds));
    }

    /**为药品信息的查询界面下拉菜单提供*/
    @GetMapping("/facList")
    @PreAuthorize("@ss.hasPermi('drug:factories:query')")
    public AjaxResult facList(){
        return AjaxResult.success(iFactoriesService.allFactories());
    }

}
